
const defaultFont = "system-ui,-apple-system,BlinkMacSystemFont,segoe ui,Roboto,Helvetica,Arial,sans-serif,apple color emoji,segoe ui emoji,segoe ui symbol"
const echartsDefaultColor = ["#c23531","#2f4554", "#61a0a8", "#d48265", "#91c7ae","#749f83", "#ca8622", "#bda29a","#6e7074","#546570", "#c4ccd3"]
const defaultTitle = {
  name: "未命名",
  style: {
    fontFamily: defaultFont,
    fontSize: "16px"
  }
}
const defaultOption = {
  title: defaultTitle,
  option: {
    xAxis:{
      gridIndex: 0,
      show: false,
      position: "bottom",
      type: "category",
      name: "",
      nameTextStyle: {
        fontFamily: defaultFont,
        fontSize: 12,
        color: "#000000"
      },
      nameLocation: "",
      data: [],
      axisLine: {
        show: true
      },
      axisTick: {
        show: true,
        length: 14
      }
    },
    yAxis: {
      show: false,
      position: "top",
      type: "value",
      name: "",
      nameLocation: "",
      nameTextStyle: {
        fontFamily: defaultFont,
        fontSize: 12,
        color: "#000000"
      },
      axisLine: {
        show: true
      },
      axisTick: {
        show: true
      }
    },
    legend: {
      show: false,
      left: "center",
      right: "auto",
      top: "top",
      bottom: "auto",
      width: "auto",
      height: "auto",
      orient: "horizontal",
      textStyle: {
        fontFamily: defaultFont,
        fontSize: 12
      }
    },
    series: [],
    backgroundColor: "transparent"
  }
}
export const fontFamilyList = [
  { name: "默认", value: defaultFont },
  { name: "微软雅黑", value: "Microsoft Yahei" },
  { name: "苹方", value: "PingFang SC" },
  { name: "黑体", value: "SimHei" },
  { name: "楷体", value: "KaiTi" },
  { name: "宋体", value: "SimSun" }
]
const dataSource = {
  state: {
    history: [],
    defaultFont,
    defaultOption,
    echartsDefaultColor,
    defaultTitle,
    fontFamilyList: [
      { name: "默认", value: defaultFont },
      { name: "微软雅黑", value: "Microsoft Yahei" },
      { name: "苹方", value: "PingFang SC" },
      { name: "黑体", value: "SimHei" },
      { name: "楷体", value: "KaiTi" },
      { name: "宋体", value: "SimSun" }
    ],
    widgetType: "",
    data: {
      xList: [],
      yList: [],
      indicatorList: [],
      filterList: [],
      option: JSON.parse(JSON.stringify(defaultOption))
    },
    currentIndex: undefined
  },
  mutations: {
    resetOption(state, data) {
      state.history = []
      state.data.option = data
    },
    setOption(state, option) {
      state.history.push(JSON.parse(JSON.stringify(state.data)))
      state.currentIndex = state.history.length - 1
      state.data.option = option
    },
    incognitoSetOption(state, option) {
      state.data.option = option
    },
    setWidget(state, obj) {
      const { type, option } = obj
      state.widgetType = type
      state.data.option = {...option}
      if (state.currentIndex !== undefined && state.currentIndex !== state.history.length - 1) {
        state.history = [...state.history.slice(0, state.currentIndex + 1), JSON.parse(JSON.stringify(state.data))]
      } else {
        state.history.push(JSON.parse(JSON.stringify(state.data)))
      }
      state.currentIndex = state.history.length - 1
    },
    clearData(state) {
      state.data = {
        xList: [],
        yList: [],
        indicatorList: [],
        filterList: [],
        type: "",
        option: JSON.parse(JSON.stringify(defaultOption))
      }
    },
    clearHistory(state) {
      state.history = []
    },
    setIndex(state, index) {
      state.currentIndex = index
      if (index < 0) {
        state.data = {
          xList: [],
          yList: [],
          indicatorList: [],
          filterList: [],
          option: JSON.parse(JSON.stringify(defaultOption))
        }
      } else {
        state.data = JSON.parse(JSON.stringify(state.history[index]))
      }
    },
    setData(state, obj) {
      const { key, value } = obj
      state.data[key] = value
    },
    setAxisData(state, obj) {
      const {option} = state.data
      state.data = {
        ...obj,
        option
      }
    }
  },
  actions: {

  }
}

export default dataSource
